<template>
  <!-- 上传厂家证照的表格modal -->
  <div>
    <basic-modal 
        :modalOpt="modal" 
        :buttonText="['','取消']"
        @closeModal="closeModal" 
        :footer="footer" 
        >
      <template #cusBtn>
          <a-button type="primary" :loading="loading_p" @click="onPassed" >{{isview?'通过':'批量通过'}}</a-button>
          <a-button type="primary"  @click="onUnPassed" >{{isview?'驳回':'批量驳回'}}</a-button>
      </template>
      <template>
        <p>操作后请提醒客户查看！</p>
        <batch-operat-btn v-if="editable" :isView="false" :addData="true" @onAddData="onAddData">
          <template #cusBtn> </template>
        </batch-operat-btn>
        <a-list-info :infoDatas="infoDatas" v-if="isview" />
        <div class="recordBox" v-if="isview">
          <a-list-table
            :columns="columns"
            :tableData="listdata"
            :isSelecttion="false"
            :loading="loading"
            :scroll="{ y: null }"
          >
          </a-list-table>
        </div>
      </template>
    </basic-modal>
    <basic-modal 
        :modalOpt="reason" 
        :buttonText="['','取消']"
        @closeModal="onCancel" 
        :footer="footer" 
        >
        <template #cusBtn>
          <a-button type="primary" @click="onSure" :loading="loadingr" >确认</a-button>
        </template>
        <template>
          <a-textarea
            v-model="reasonVal"
            placeholder="请输入驳回理由"
          />
        </template>
    </basic-modal>
  </div>
</template>

<script>
import basicModal from '@/views/app/biz/common/basicModal'
import aListTable from '@/views/app/biz/common/aListTable'
import aListInfo from '@/views/app/biz/common/aListInfo'
import batchOperatBtn from '@/views/app/biz/common/batchOperatBtn'

// import { getInfoByPid as getSupplierInfoByPid } from '@/views/app/biz/supplier/supplier'
import { getLicenseByPid, auditLicense  } from './regisLicense'
import {getZZImgByzzpid} from '@/views/app/biz/licenseImg/licenseImg'

export default {
  components: {
    basicModal,
    aListTable,
    batchOperatBtn,
    aListInfo
  },
  data() {
    return {
      footer:undefined,
      editable:false,
      loading: false,
      isbatch:false,
      isview:true,
      reason:{
        modalVisible: false,
        modalTitle: '请填写驳回理由',
        modalWidth: '400px',
      },
      loadingr:false,
      loading_p:false,
      reasonVal:'',
      modal: {
        modalVisible: false,
        modalTitle: '审核证照',
        modalWidth: '1000px',
      },
      infoDatas:[
        { title: '供应商代码', id: 'dengJidwdm',  value:''},
        { title: '供应商名称', id: 'dengJidwmc',  value:''},  
        { title: '证类型', id: 'zhengZhaolx',value:''},
        { title: '证照号', id: 'zhengZhaohm',value:''},
        { title: '产品名称', id: 'chanPinmc',value:''},
        { title: '颁发机构', id: 'banFajg',value:''},
        { title: '有效期止', id: 'youXiaoqz',value:''},
      ],
      columns: [
        { title: '证照类型', dataIndex: 'zhengZhaolxmc',  key: 'zhengZhaolxmc', align: 'center' },
        { title: '证照说明', dataIndex: 'zhengZhaosm', key: 'zhengZhaosm', align: 'center' },
        {title: '证照图片',dataIndex: 'zhengZhaotpurl',key: 'zhengZhaotpurl',align: 'center',scopedSlots: { customRender: 'img' },},
      ],
    //   columns: [
    //     { title: '证照类型', dataIndex: 'zhengJielx', width: 80, key: 'zhengJielx', align: 'center' },
    //     { title: '证照号', dataIndex: 'zhengJiehm', key: 'zhengJiehm', align: 'center' },
    //     { title: '证照开始日期', dataIndex: 'youXiaoqs', key: 'youXiaoqs', align: 'center' },
    //     { title: '证照结束日期', dataIndex: 'youXiaoqz', key: 'youXiaoqz', align: 'center' },
    //     { title: '证照备注', dataIndex: 'zhengJiebz', key: 'zhengJiebz', align: 'center' },
    //     {title: '证照图片',dataIndex: 'zhengJietp',key: 'zhengJietp',align: 'center',scopedSlots: { customRender: 'img' },},
    //   ],
      listdata: [],
      listKey: 0,
      pid: '',
      zzPid:'',
      sysSeq: 0,
      loadings: false,
    }
  },
  computed: {},
  mounted() {},
  methods: {
    // 点击了上传
    addModal(val) {
      this.modal.modalVisible = true
      this.modal.modalTitle = '审核证照'
      this.listdata = []
      this.pid = val
      this.getSBypid(val)
    },
    // 点击了审核editabl传true或者未传值表示可编辑，传false表示不可编辑
    editModal(val,val2) {
      this.pid = val
      this.zzPid = val2
      this.listdata = []
      this.modal.modalVisible = true
      this.modal.modalTitle = '审核证照'
      this.getSBypid(val)
      this.getZZImgBypid(val2)
      
    },
    // 点击了批量审核
    batchModal(val) {
      this.recordArr=JSON.parse(JSON.stringify(val))
      this.modal.modalVisible = true
      this.modal.modalTitle = '批量审核证照'
      this.modal.modalWidth="400px"
      this.isview=false
      this.isbatch=true
    },
    // 关闭模态详细框
    closeModal() {
      this.modal.modalVisible = false
      this.$emit('close')
    },
    closeModal2() {
      this.modal.modalVisible = false
      this.$emit('ok')
    },
    //
    onAddData() {
      this.$refs.addSellerLicChildModal.addModal(this.pid)
    },

    // 审核通过
    onPassed() {
      this.loading_p=true;
      let reqArr=[]
      if(this.isbatch){
        for (const item of this.recordArr) {
          item.zhuangTai='已审核'
          item.unPassReason=null
          reqArr.push(this.saveObjInfo(item))
        }
      }else{
        let subData = {
          sysSeq: this.sysSeq,
          pid: this.pid,
          zhuangTai:'4',
          unPassReason:null
        }
        reqArr.push(this.saveObjInfo(subData))
      }
      this.dealResult(reqArr,true)
    },
    // 审核不通过onUnPassed
    onUnPassed() {
      this.reason.modalVisible=true;
    },
    onSure(){
      this.loadingr=true;
      let reqArr=[]
      if(this.isbatch){
        for (const item of this.recordArr) {
          item.zhuangTai='已驳回'
          item.unPassReason=this.reasonVal
          reqArr.push(this.saveObjInfo(item))
        }
      }else{
        let subData = {
          sysSeq: this.sysSeq,
          pid: this.pid,
          zhuangTai:'5',
          unPassReason:this.reasonVal
        }
        reqArr.push(this.saveObjInfo(subData))
      }
      this.dealResult(reqArr,false)
      
    },
    onCancel(){
      this.loadingr=false;
      this.loading_p=false;
      this.reason.modalVisible=false;
    },
    // 保存详细信息
    async saveObjInfo(val) {
      let res = await auditLicense(val)
      return res
    },
    // 保存详细信息
    dealResult(reqArr,isPass) {
      let tips=''
      if(isPass){
        tips='已审核通过'
      }else{
        tips='已成功驳回'
      }
      return Promise.all(reqArr).then(res => {
        this.loading = false
        console.log(res)
        for (const item of res) {
          if (item && item.code && item.code === 'C1' && item.data) {
            this.$message.success({ content: '证照号码:'+item.data.zhengZhaohm+tips, duration: 2 })
          } else {
            this.$message.success({ content: '证照号码:'+item.data.zhengZhaohm+'操作失败，请稍后再试', duration: 2 })
          }
        }
        this.onCancel()
        this.closeModal2()
      }).catch(err => {
        this.$message.error({ content:'请求出错:'+err, duration: 3 })
      })
    },
    // 得到初始化信息
    async getSBypid(val) {
      let res = await getLicenseByPid(val)
      if (res && res.code && res.code === 'C1') {
        this.sysSeq = res.data.sysSeq
        for (const key in res.data) {
            for (const item of this.infoDatas) {
                if(key==item.id){
                    item.value=res.data[key]
                }
            }
        }
      }
    },
    // 得到初始化证照图片信息
    async getZZImgBypid(val2) {
      this.loading=true
      let pa = {
        zhengZhaoPid: val2,
      }
      let res = await getZZImgByzzpid(pa)
      if (res && res.code && res.code === 'C1' && res.data) {
        const resdata = res.data
        for (let item of resdata) {
          this.listdata.push({
              key: item.pid,
              ...item
              }
          )
        }
        this.loading=false
      }
      console.log( this.listdata)
    },
  },
}
</script>

<style>
.recordBox {
  width: 100%;
  min-height: 80px;
}
</style>